import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/network/Loading.dart';
import 'package:flutter_app/utils/image_file_info_utils.dart';
import 'package:flutter_app/utils/path_provider_utils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:get/get.dart';

import 'photo_gallery_list/photo_gallery_list_view.dart';
import 'photo_gallery_state.dart';

class PhotoGalleryLogic extends GetxController with GetSingleTickerProviderStateMixin {
  final PhotoGalleryState state = PhotoGalleryState();

  late String dirName;

  late TabController tabController;

  late List<BadgeTab> badgeTab = [];

  late List<Widget> widgets = [];

  @override
  void onInit() {
    super.onInit();
    dirName = Get.arguments ?? "";

    tabController = TabController(length: 3, vsync: this);

    state.tabs.forEach((tab) {
      badgeTab.add(BadgeTab(text: tab));
    });

    for (int i = 0; i < state.tabs.length; i++) {
      widgets.add(PhotoGalleryListPage(title: '可视化采集资料列表',));
    }
  }

  @override
  void onReady() {
    super.onReady();
    // 初始化 全部的 资源集合
    getImageFileInfo(dirName, 0);

    tabController.addListener(() {
      // 切换后重置 是否全选 默认false
      state.isAll = false;
      getImageFileInfo(dirName, tabController.index);
    });
  }

  Future<void> getImageFileInfo(String dirName, int index) async {
    Loading.show();
    List<ImageFileInfo> imageFileInfo = await ImageFileInfoUtils().getFileInfoInGroupId(dirName);
    List<PhotoGalleryInfo> list = [];
    for (ImageFileInfo info in imageFileInfo) {
      if (index == 2) {
        if (info.type != "video") {
          continue;
        }
      } else if (index == 1) {
        if (info.type != "image") {
          continue;
        }
      }
      PhotoGalleryInfo photoGalleryInfo = new PhotoGalleryInfo();
      photoGalleryInfo.list = [];
      bool add = true; // 是否添加 标题
      list.forEach((element) {
        // 当title 有相同的时候 不添加
        if (info.time == element.title) {
          add = false;
          return;
        }
      });
      // 添加 标题
      if (add) {
        photoGalleryInfo.title = info.time;
        list.add(photoGalleryInfo);
      }
      // 把title 相同 添加 同一个 List中
      list.where((element) => element.title == info.time).forEach((element) {
        element.list?.add(info);
      });
    }
    Loading.dismiss();
    state.entity = list;
    update();
  }

  // 编辑按钮
  void edit() {
    state.isEdit.value = !state.isEdit.value;
    if (state.isEdit.value) {
      state.edit = "完成";
    } else {
      state.edit = "编辑";
    }
    update();
  }

  // 全选
  void all() {
    state.isAll = !state.isAll;
    state.entity?.forEach((entity) {
      entity.list?.forEach((media) {
        media.isChose.value = state.isAll;
      });
    });
    update();
  }

  // 删除按钮
  Future<void> delete() async {
    for (PhotoGalleryInfo entity  in state.entity!) {
      List<ImageFileInfo> list = [];
      for (ImageFileInfo info in entity.list!) {
        if (info.isChose.value) {
          list.add(info);
        }
      }
      for (ImageFileInfo info in list) {
        entity.list?.remove(info);
        await ImageFileInfoUtils().delete(info);
        await PathProviderUtils().deleteFile(info.path ?? "");
      }
    }
    toast.show("删除完成");
    update();
  }

  @override
  void onClose() {
    super.onClose();
    tabController.dispose();
  }
}
